package com.srv.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.srv.dao.DAOCidade;
import com.srv.dao.DAOGradeHoraria;
import com.srv.dao.DAOReserva;
import com.srv.pojo.Cidade;
import com.srv.pojo.GradeHoraria;
import com.srv.pojo.Reserva;

/**
 * Servlet implementation class EfetuarReservaServlet
 */
@WebServlet("/ModificarReservaServlet")
public class ModificarReservaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ModificarReservaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession();
			
			String strOperacao = "";
			Reserva reserva = new Reserva();
			DAOReserva daoReserva = new DAOReserva();

			DAOGradeHoraria daoGradeHoraria = new DAOGradeHoraria();
			List<GradeHoraria> horarios = daoGradeHoraria.getGradeHorariaList();
			session.setAttribute("horarios", horarios);
			
			DAOCidade daoCidade = new DAOCidade();
			List<Cidade> cidades = daoCidade.getCidadeList();
			session.setAttribute("cidades", cidades);

			reserva = daoReserva.getReservaByRescode(Integer.parseInt(request.getParameter("rescode").trim()));
			
			List<Reserva> reservas = daoReserva.getReservaList(reserva.getDataPartida(), reserva.getHoraPartida(), reserva.getVeiculo(), reserva.getCidadeOrigem(), reserva.getCidadeDestino());
			reservas.add(reserva); //Marca como poltrona jah selecionada
			session.setAttribute("veiculo", reserva.getVeiculo());
			session.setAttribute("auxReservas", reservas);
			strOperacao = "?tipo_operacao=alterar";
			
			session.setAttribute("reserva", reserva);
			
			RequestDispatcher view = request
					.getRequestDispatcher("efetuarReserva.jsp"+strOperacao);
			view.forward(request, response);
			
		} catch (Exception ex) {
			System.out.println("Ocorreu um erro " + ex.getMessage());
		}

	}

}
